package com.qs.eggs.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.bean.CollectBean;
import com.qs.eggs.view.activity.CollectActivity;
import com.qs.eggs.view.activity.FootPrintActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class CollectAdapter extends SimpleAdapter<CollectBean> implements BaseAdapter.OnItemClickListener {
    BaseViewHolder viewHoder;
    private int action;
    private String proid,proname,price,type;


    public CollectAdapter(Context context, List<CollectBean> datas, TextView edit) {
        super(context, R.layout.item_collect, datas);

        setOnItemClickListener(this);
    }

    @Override
    protected void convert(BaseViewHolder viewHoder, final CollectBean item, final int position) {
        this.viewHoder=viewHoder;
        viewHoder.getTextView(R.id.tv_proname).setText(item.getProname());
        viewHoder.getTextView(R.id.tv_proprice).setText("¥"+item.getSaleprice()+"");

        SimpleDraweeView draweeView = (SimpleDraweeView) viewHoder.getView(R.id.propic);
        draweeView.setImageURI(Constants.URL_BASE+"productimages/"+item.getAutoname());

        proid=item.getProid();
        proname=item.getProname();
        price=item.getSaleprice();
        type=item.getType();

        final  HashMap<String,String> map=new HashMap<String, String>();
        map.put("proid",proid);
        map.put("proname",proname);
        map.put("price",price);
        map.put("count","1");
        map.put("type",type);
        map.put("specification","");
        viewHoder.getImageView(R.id.img_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(action==1){
                    mDel(item.getCollectid());
                }else {
                    addCart(map);
                }
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
    }

   //执行加入购物车操作
    private void addCart(Map map){
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"shoppingcart?action=addShoppingCart")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("加入购物车=="+s);
                        if (s.equals("\"true\"")){
                            Toast.makeText(context,"加入购物车成功",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(context,"加入购物车失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    //执行删除操作
    private void mDel(String s){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("ids",s);
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"collect?action=deleteCollection")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("删除足迹=="+s);
                        if (s.equals("\"true\"")) {
                            Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                            CollectActivity.instance.finish();
                            Intent intent = new Intent(context, CollectActivity.class);
                            context.startActivity(intent);
                        }
                    }
                });
    }

    public void setEdit(int action){
        this.action=action;
        if(action==1){
            viewHoder.getImageView(R.id.img_edit).setImageResource(R.mipmap.icon_del);
        }else {
            viewHoder.getImageView(R.id.img_edit).setImageResource(R.mipmap.icon_cart);

        }
    }
}
